package top.jamewen.framework.sbmp.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import top.jamewen.framework.sbmp.common.api.CommonPage;
import top.jamewen.framework.sbmp.common.api.CommonResult;
import top.jamewen.framework.sbmp.common.model.UmsAdmin;
import top.jamewen.framework.sbmp.common.model.UmsRole;
import top.jamewen.framework.sbmp.model.qo.UpdateAdminPasswordQO;
import top.jamewen.framework.sbmp.service.UmsAdminService;
import top.jamewen.framework.sbmp.service.UmsMenuService;
import top.jamewen.framework.sbmp.service.UmsRoleService;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author jamewen
 * @since 2021-01-11
 */
@Slf4j
@RestController
@Api(tags = "后台用户管理模块")
@RequestMapping("/admin")
public class UmsAdminController {
    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private UmsRoleService roleService;
    @Autowired
    private UmsMenuService menuService;

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<Map<String, Object>> getAdminInfo(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
        Map<String, Object> data = new HashMap<>();
        data.put("username", umsAdmin.getUsername());
        data.put("menus", menuService.getMenuList(umsAdmin.getId()));
        data.put("icon", umsAdmin.getIcon());
        List<UmsRole> roleList = roleService.getRoleList(umsAdmin.getId());
        if(CollUtil.isNotEmpty(roleList)){
            List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
            data.put("roles",roles);
        }
        return CommonResult.success(data);
    }

    @ApiOperation(value = "登出功能")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public CommonResult<String> logout() {
        return CommonResult.success(null);
    }

    @ApiOperation("用户列表")
    @RequestMapping(method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsAdmin>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsAdmin> adminList = adminService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(adminList));
    }

    @ApiOperation("获取指定用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<UmsAdmin> getItem(@PathVariable Long id) {
        UmsAdmin admin = adminService.getById(id);
        return CommonResult.success(admin);
    }

    @ApiOperation("修改指定用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public CommonResult<Boolean> update(@PathVariable Long id, @RequestBody UmsAdmin admin) {
        try {
            return CommonResult.success(adminService.update(id, admin));
        }catch (Exception e){
            log.error("修改密码失败：{}",e.getMessage());
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("修改指定用户密码")
    @RequestMapping(value = "/password", method = RequestMethod.PUT)
    public CommonResult<String> updatePassword(@Validated @RequestBody UpdateAdminPasswordQO updateAdminPasswordQo) {
        int status = adminService.updatePassword(updateAdminPasswordQo);
        if (status > 0) {
            return CommonResult.success("修改密码成功");
        } else if (status == -1) {
            return CommonResult.failed("提交参数不合法");
        } else if (status == -2) {
            return CommonResult.failed("找不到该用户");
        } else if (status == -3) {
            return CommonResult.failed("旧密码错误");
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除指定用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public CommonResult<String> delete(@PathVariable Long id) {
        boolean success = adminService.delete(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改帐号状态")
    @RequestMapping(value = "/status/{id}", method = RequestMethod.PUT)
    public CommonResult<String> updateStatus(@PathVariable Long id,@RequestParam(value = "status") Integer status) {
        UmsAdmin umsAdmin = new UmsAdmin();
        umsAdmin.setStatus(status);
        boolean success = adminService.update(id,umsAdmin);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

}

